package com.bootdo.fx.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.fx.dao.UserAddressDao;
import com.bootdo.fx.domain.UserAddressDO;
import com.bootdo.fx.service.UserAddressService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserAddressServiceImpl implements UserAddressService {
	@Autowired
	private UserAddressDao userAddressDao;

	@Override
	public UserAddressDO get(Long addrId) {
		return userAddressDao.get(addrId);
	}

	@Override
	public List<UserAddressDO> list(Map<String, Object> map) {
		return userAddressDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		return userAddressDao.count(map);
	}

	@Override
	public int save(UserAddressDO userAddress) {
		return userAddressDao.save(userAddress);
	}

	@Override
	public int update(UserAddressDO userAddress) {
		return userAddressDao.update(userAddress);
	}

	@Override
	public int remove(Long addrId) {
		return userAddressDao.remove(addrId);
	}

	@Override
	public int batchRemove(Long[] addrIds) {
		return userAddressDao.batchRemove(addrIds);
	}


	@Override
	@Transactional
	public int setDefualtAddress(String userId, String addressId) {

		Map<String, Object> map = new HashMap<>();
		map.put("userId", userId);
		map.put("addrIsDefault", 1);
		List<UserAddressDO> list = userAddressDao.list(map);
		for (UserAddressDO ua : list) {
			ua.setAddrIsDefault(0);
			userAddressDao.update(ua);
		}
		UserAddressDO u = new UserAddressDO();
		u.setAddrId(Long.parseLong(addressId));
		u.setAddrIsDefault(1);
		int num = userAddressDao.update(u);
		return num;
	}

}